﻿//-----------------------------------------------------------------------
// <copyright file="HtmlToXamlTestHelper.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.XamlUtility.Test
{
    using System.Windows.Documents;

    /// <summary>
    /// Helper methods for texsting xaml documents
    /// </summary>
    public static class HtmlToXamlTestHelper
    {
        /// <summary>
        /// Find a child inline of the specified type
        /// </summary>
        /// <typeparam name="T">
        /// the type to find
        /// </typeparam>
        /// <param name="inline">
        /// the inline to search under
        /// </param>
        /// <returns>
        /// the found element
        /// </returns>
        public static T FindChild<T>(this Inline inline) where T : Inline
        {
            if (inline is T)
            {
                return (T)inline;
            }

            if (inline is Span)
            {
                Span span = (Span)inline;
                foreach (Inline child in span.Inlines)
                {
                    if (child is T)
                    {
                        return (T)child;
                    }
                }

                foreach (Inline child in span.Inlines)
                {
                    Inline result = FindChild<T>(child);
                    if (result != null)
                    {
                        return (T)result;
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Does the inline have a child of the specified type
        /// </summary>
        /// <typeparam name="T">
        /// the type to find
        /// </typeparam>
        /// <param name="inline">
        /// the inline to search under
        /// </param>
        /// <returns>
        /// true if the child element was found
        /// </returns>
        public static bool HasChild<T>(this Inline inline) where T : Inline
        {
            return FindChild<T>(inline) != null;
        }
    }
}
